/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.toto.login;

import com.toto.loginAccount.LoginLocal;
import com.toto.loginAccount.LoginLocalHome;
import com.toto.loginAccount.object.Login;
import com.toto.loginAccount.object.Permission;
import java.lang.String;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Administrator
 */
public class LoginServicesBean implements SessionBean
{

  private SessionContext sessionContext;
  private LoginLocalHome loginLocalHome;

  public void setSessionContext(SessionContext sessionContext) throws
    EJBException,
    RemoteException
  {
    this.sessionContext = sessionContext;
    try {
      Context ctx = new InitialContext();
      Object obj = ctx.lookup("java:comp/env/ejb/Login");
      loginLocalHome = (LoginLocalHome) obj;
    }
    catch (NamingException ex) {
      Logger.getLogger(LoginServicesBean.class.getName()).log(Level.SEVERE, null,
        ex);
    }
  }

  public void ejbCreate() throws CreateException, RemoteException
  {
  }

  public void ejbRemove() throws EJBException, RemoteException
  {
  }

  public void ejbActivate() throws EJBException, RemoteException
  {
  }

  public void ejbPassivate() throws EJBException, RemoteException
  {
  }

  public boolean isRightAccount(String userId, String password) throws RemoteException
  {
    try {
      LoginLocal local = loginLocalHome.findByPrimaryKey(userId);
      if (local != null) {
        String realPassword = local.getPassword();
        if (password.trim().equals(realPassword.trim())) {
          return true;
        }
      }
    }
    catch (FinderException ex) {
      return false;
    }
    return false;
  }

  public void addLoginAccount(String userId, String password) throws RemoteException
  {
    Permission perm = new Permission();
    addLoginAccount(userId, null, password, perm.LEVEL3);
  }
  public void addLoginAccount(String userId, String name, String password) throws RemoteException
  {
      this.addLoginAccount(userId, name, password, "C");
  }

  public void addLoginAccount(String userId, String name, String password, String permission) throws RemoteException
  {
    try {
        Set userIdSet = getAllUserId();
        Iterator it = userIdSet.iterator();
        while(it.hasNext())
        {
            String existUserId = (String) it.next();
            if (userId.equals(existUserId))
            {
                System.out.println("插入失败，用户名已存在！");
                return;
            }
        }
      loginLocalHome.create(userId, name, password, permission);
    }
    catch (CreateException ex) {
      Logger.getLogger(LoginServicesBean.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
  public void addLoginAccount(Login login)
  {

  }

  public List getAllName() throws RemoteException
  {
    List<String> nameList = new ArrayList<String>();
    try {
      Collection collection = loginLocalHome.findAllAccount();
      Iterator it = collection.iterator();
      while (it.hasNext()) {
        LoginLocal local = (LoginLocal) it.next();
        String name = local.getName();
        nameList.add(name);
      }
    }
    catch (FinderException ex) {
      Logger.getLogger(LoginServicesBean.class.getName()).log(Level.SEVERE, null, ex);
    }
    return nameList;
  }

  public Set getAllUserId()
  {
    Set<String> userIdSet = new HashSet<String>();
    try {
      Collection coll = loginLocalHome.findAllAccount();
      Iterator it = coll.iterator();
      while (it.hasNext()) {
        LoginLocal local = (LoginLocal) it.next();
        String userId = local.getUserId();
        userIdSet.add(userId);
      }
    }
    catch (FinderException ex) {
      Logger.getLogger(LoginServicesBean.class.getName()).log(Level.SEVERE, null, ex);
    }
    return userIdSet;
  }

  public void deleteAccount2(String userId)
  {
    try {
      try {
        loginLocalHome.findByPrimaryKey(userId).remove();
      }
      catch (RemoveException ex) {
        Logger.getLogger(LoginServicesBean.class.getName()).log(Level.SEVERE, null, ex);
      }
      catch (EJBException ex) {
        Logger.getLogger(LoginServicesBean.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    catch (FinderException ex) {
      Logger.getLogger(LoginServicesBean.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public void deleteAccount(String userId)
  {
    try {
      loginLocalHome.remove(userId);
    }
    catch (RemoveException ex) {
      Logger.getLogger(LoginServicesBean.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (EJBException ex) {
      Logger.getLogger(LoginServicesBean.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  
  public String getUserId(String userId) throws FinderException, RemoteException
  {
      LoginLocal local = loginLocalHome.findByPrimaryKey(userId);
      return local.getUserId();
     
  }
  public String getPassword(String userId) throws FinderException, RemoteException
  {
      LoginLocal local = loginLocalHome.findByPrimaryKey(userId);
      return local.getPassword();
     
  }
  public String getName(String userId) throws FinderException, RemoteException
  {
      LoginLocal local = loginLocalHome.findByPrimaryKey(userId);
      return local.getName();
  }
  public void changePassword(String userId, String newPassword)
  {
      try {
          LoginLocal local = loginLocalHome.findByPrimaryKey(userId);
          local.setPassword(newPassword);
      } catch (FinderException ex) {
          Logger.getLogger(LoginServicesBean.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  public void changePermission(String userId, String permission) throws FinderException, RemoteException
  {
      LoginLocal local = loginLocalHome.findByPrimaryKey(userId);
      local.setPermission(permission);
  }
  public String getPermission(String userId) throws FinderException, RemoteException
  {
      return loginLocalHome.findByPrimaryKey(userId).getPermission();
  }
}
